package com.no1ikun.service;

import com.no1ikun.bean.Punishment;
import com.no1ikun.util.JDBCUtil;

import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

public class PunishmentImp extends Punishment {
    public PunishmentImp(String sno, String ptype, String date) {
        super(sno, ptype, date);
    }

    public PunishmentImp() {
        super();
    }

    //插入奖惩信息
    public int insertPunishment(String sno, String ptype, String date) {
        String sql = "INSERT INTO RewardPunishments(sno,ptype,time) VALUES (?,?,?);";
        int i = JDBCUtil.executeUpdate(sql, sno,ptype,date);
        return i;
    }

    //修改奖惩信息
    public int updateByNumberOrSno(PunishmentImp item) {
        String sql = "UPDATE RewardPunishments SET  ptype = ?,  time = ? WHERE sno=? and time=?";
        System.out.println(sql);
        System.out.println(item.getDate());
        int i = JDBCUtil.executeUpdate(sql, item.getPtype(),item.getDate(),item.getSno(),item.getDate());
        return i;
    }
    //获取奖惩信息 返回一个奖惩对象
    public PunishmentImp selectBySno(String sno) {
        String sql = "select *  from RewardPunishments where sno=?";
        ResultSet rs = JDBCUtil.executeQuery(sql, sno);
        try {
            if (rs.next()) {
                //获取数据库的数据
                PunishmentImp item = new PunishmentImp();
                item.setSno(rs.getString("sno"));
                item.setPtype(rs.getString("ptype"));
                item.setDate(rs.getString("time"));
                return item;
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        return null;
    }
    public Object[][] getData() {
        //定义sql语句
        String sql = "select * from RewardPunishments";
        return getObjects(sql);
    }
    public Object[][] selectPushBySno(String sno) {
        //更具关键字查询
        String sql = "select * from RewardPunishments where sno="+"'"+sno+"'";
        System.out.println(sql);
        return getObjects(sql);
    }
    //封装一个查询数据的方法
    public Object[][] getObjects(String sql) {
        ResultSet rs = JDBCUtil.executeQuery(sql);
//        创建一个集合用类存放获取到的数据
        List<PunishmentImp> list = new ArrayList<>();
        try {
            while (rs.next()) {
                //获取数据库的数据
                PunishmentImp item = new PunishmentImp();
                item.setSno(rs.getString("sno"));
                item.setPtype(rs.getString("ptype"));
                item.setDate(rs.getString("time"));
                list.add(item);
            }
        } catch (SQLException throwables) {
            throwables.printStackTrace();
        }
        Object[][] stus = new Object[list.size()][9];
        for (int i = 0; i < stus.length; i++) {
            stus[i][0] = list.get(i).getSno();
            stus[i][1] = list.get(i).getPtype();
            stus[i][2] = list.get(i).getDate();
        }
        return stus;
    }

    public static void main(String[] args) {
        PunishmentImp item = new PunishmentImp("S2023402", "警告", "2022-09-01");
        item.insertPunishment(item.getSno(), item.getPtype(), item.getDate());
        PunishmentImp newitem=item.selectBySno("S2023101");
        System.out.println(newitem);
        item.getData();
    }
}
